/*
 * @Author: your name
 * @Date: 2021-04-21 14:58:53
 * @LastEditTime: 2021-04-21 15:48:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \provider_demo\lib\comsumer_demo.dart
 */

import 'package:flutter/material.dart';
import 'package:provider_demo/counter2_model.dart';
import 'counter_model.dart';
import 'package:provider/provider.dart';
import 'test_icon.dart';

class ConsumerTabPage1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 使用Provider.of 获取资源，可以得到资源暴露的数据的读写接口
    // 注意滥用Provider.of方法，会导致当数据更新时，页面的其他的子widget也会跟着刷新，造成性能问题
    final _counter = Provider.of<CounterModel>(context);
    final textSize = Provider.of<double>(context);
    print('使用Provider.of,ConsumerTabPage1页面都从新渲染了');
    return Scaffold(
      appBar: AppBar(
        title: Text('First Page'),
      ),
      body: Center(
        child: Text(
          'Counter: ${_counter.counter}',
          style: TextStyle(fontSize: textSize),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => ConsumerTabPage2())),
        child: Icon(Icons.navigate_next),
      ),
    );
  }
}

class ConsumerTabPage2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('ConsumerTabPage2');
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Column(
        children: [
          Consumer2<CounterModel, double>(
              builder: (context, CounterModel counter, double textSize, _) =>
                  Text('Counter: ${counter.counter}',
                      style: TextStyle(fontSize: textSize))),
          Consumer3<CounterModel, CounterModel2, double>(
              builder: (context, CounterModel counter, CounterModel2 counter2,
                      double textSize, _) =>
                  Text(
                      'counter: ${counter.counter} + counter2: ${counter2.count}',
                      style: TextStyle(fontSize: textSize))),
          Consumer<CounterModel2>(
            builder: (context, conter, child) {
              return Container(
                width: 100,
                height: 40,
                color: Colors.blue,
                child: InkWell(
                  onTap: conter.increment,
                  child: child,
                ),
              );
            },
            child: TestIcon(),
          )
        ],
      ),
      floatingActionButton: Consumer<CounterModel>(
        builder: (context, CounterModel counter, child) {
          print('child===>${child}');
          return FloatingActionButton(
            onPressed: counter.increment,
            child: child,
          );
        },
        child: TestIcon(),
      ),
    );
  }
}
